package com.e9ab98e991ab.moduler.ui.guide;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.e9ab98e991ab.libcommon.base.BaseActivity;
import com.e9ab98e991ab.libcommon.utils.SPSingleData;
import com.e9ab98e991ab.libcommon.utils.StatusBarUtil;
import com.e9ab98e991ab.moduler.ui.MainActivity;
import com.e9ab98e991ab.moduler.R;
import com.e9ab98e991ab.moduler.ui.guide.indicator.CirclePageIndicator;


import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

/** 
 * @author gaoxin 18-12-20 上午9:51
 * @version V1.0.0
 * @name SplashActivity 引导页MP4播放
 */
public class SplashActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private ExtendedViewPager mVpVideo;
    private TextView mTvEnter;
    private CirclePageIndicator mViewPagerIndicator;
    private boolean mVisible;
    private ViewPagerAdapter mVpAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }



    @Override
    public void initSet() {
        StatusBarUtil.immersiveStatusBar(this);

        mVisible = true;

        mVpVideo = (ExtendedViewPager) findViewById(R.id.vp_video);
        mTvEnter = (TextView) findViewById(R.id.tv_enter);
        mViewPagerIndicator = (CirclePageIndicator) findViewById(R.id.view_pager_indicator);
        mVpAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        mVpVideo.setAdapter(mVpAdapter);
        mVpVideo.setOffscreenPageLimit(4);
        mTvEnter.setOnClickListener(this);
        mViewPagerIndicator.setViewPager(mVpVideo);
    }

    @Override
    public void initDate() {
        if (SPSingleData.contains(this, "SplashActivity") ) {
            Boolean cropImagePath = (Boolean) SPSingleData.getObjectForKey(this, "SplashActivity", false);
            if (cropImagePath){
            startActivity(new Intent(this,MainActivity.class));
            finish();
            }
        }
    }


    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final int[] videoRes;
        private final int[] slogonImgRes;

        public ViewPagerAdapter(FragmentManager paramFragmentManager) {
            super(paramFragmentManager);
            this.videoRes = new int[]{R.raw.splash_1, R.raw.splash_2, R.raw.splash_3, R.raw.splash_4};
            this.slogonImgRes = new int[]{R.drawable.slogan_1, R.drawable.slogan_2, R.drawable.slogan_3, R.drawable.slogan_4};
        }

        @Override
        public int getCount() {
            return this.videoRes.length;
        }

        @Override
        public Fragment getItem(int paramInt) {
            VideoItemFragment videoItemFragment = new VideoItemFragment();
            Bundle localBundle = new Bundle();
            localBundle.putInt("position", paramInt);
            localBundle.putInt("videoRes", this.videoRes[paramInt]);
            localBundle.putInt("imgRes", this.slogonImgRes[paramInt]);
            videoItemFragment.setArguments(localBundle);
            if (paramInt < getCount()) {
                return videoItemFragment;
            }
            throw new RuntimeException("Position out of range. Adapter has " + getCount() + " items");
        }
    }


    public void next(int positon) {
        int i = this.mVpVideo.getCurrentItem();
        if (positon == i) {
            positon += 1;

            this.mVpVideo.setCurrentItem(positon, true);

            if (positon == 4) {
                SPSingleData.setObject(this, "SplashActivity", true);
                startActivity(new Intent(this,MainActivity.class));
                finish();

            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ((VideoItemFragment) (mVpAdapter.getItem(position))).play();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }



    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_enter) {
            SPSingleData.setObject(this, "SplashActivity", true);
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }
    }
}
